<?php

abstract class AbstractDbUtils
{
 
    
    abstract protected function get_pdo_connection();
 	
 	public static function getInstance(){
 		$classPath=DB_IMPLEMENTS_PATH;
 		$className=DB_IMPLEMENTS_CLASS;

 	    $file=APP_PATH.DIRECTORY_SEPARATOR.DB_IMPLEMENTS_PATH.DIRECTORY_SEPARATOR.DB_IMPLEMENTS_CLASS.".php";
        if (is_readable($file)) {
       		require_once $file; 
	    }
        return new ReflectionClass($className);
 	}
 	
 	public static function pdo_connection($key=CONNECTION_KEY,$transaction=false ){
	    if(empty($_REQUEST[$key])){
	    	$class = AbstractDbUtils::getInstance();
	    	$instance = $class->newInstance();
	    	$method = $class->getMethod("get_pdo_connection");
	    	$conn = $method->invoke($instance);
            if($conn!=null && $transaction== true){
            	$conn->beginTransaction();
            }
            $_REQUEST[$key] = $conn;
        }
        return $_REQUEST[$key];
	}


	public static function GetOpenedConnection($key =CONNECTION_KEY,$createNew=false){
	    $conn = null;
	    if(!empty($_REQUEST[$key])){
            $conn = $_REQUEST[$key] ;
        }
        if($conn == null && $createNew == true){
        	$conn =  AbstractDbUtils::pdo_connection($key);
        }
        return $conn;
	}


    
}
 	
?>